<?php

namespace App\Http\Controllers\V1;

use App\Http\Controllers\ApiBaseController;
use App\Http\Controllers\Controller;
use App\Models\CzProductBlack;
use Illuminate\Http\Request;

class CzProductBlackController extends ApiBaseController
{
    //
    public function getList(Request $request)
    {
        $params = $request->all();
        $list = CzProductBlack::filter($params)->with(['spec', 'product'])->orderBy('id', 'desc')->paginate($this->pageSize);
        return success(['data' => page_format($list)]);
    }
    public function add(Request $request)
    {
        $params = $request->only(['status', 'spec_id', 'product_id', 'limit_num', 'remark']);
        $check = validator($params, [
            "spec_id" => "required",
            // "mobiles" => "required",
        ]);
        if ($check->fails()) {
            return error($check->errors()->first());
        }
        // if (!is_array($params['mobiles'])) {
        //     return error('格式错误');
        // }
        // 循环创建
        // foreach ($params['mobiles'] as $key => $val) {
        //     $params['mobile'] = $val;
        //     CzProductBlack::create($params);
        // }
        CzProductBlack::create($params);
        return success();
    }
    public function change(Request $request)
    {
        $params = $request->only(['id', 'status', 'spec_id', 'product_id', 'limit_num', 'remark']);
        $check = validator($params, [
            "spec_id" => "required",
            // "mobile" => "required",
            "id" => "required"
        ]);
        if ($check->fails()) {
            return error($check->errors()->first());
        }
        $res = CzProductBlack::where('id', $params['id'])->update($params);
        return success(['data' => $res]);
    }
    public function del(Request $request)
    {
        $ids = request('ids');
        if (!$ids) {
            return error('ID不能为空');
        }
        if (!is_array($ids)) {
            $ids = [$ids];
        }
        $res = CzProductBlack::destroy($ids);
        return success(['data' => $res]);
    }
}
